package com.zheng.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zheng.common.Result;
import com.zheng.entity.Booking;
import com.zheng.entity.User;
import com.zheng.entity.UserShop;
import com.zheng.service.BookingService;
import com.zheng.service.UserShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/booking")
public class BookingController {

    @Autowired
    private BookingService bookingService;

    @Autowired
    private UserShopService userShopService;

    @GetMapping
    public Result<?> selectAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String search, @RequestParam String option){

        Page<Booking> bookingPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Booking> bookingQueryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(search)){
            bookingQueryWrapper.like(option,search);
        }
        Page<Booking> page = bookingService.page(bookingPage, bookingQueryWrapper);
        return Result.success(page);
    }

    @PutMapping
    public Result<?> update(@RequestBody Booking booking,@RequestParam int purchasedNumber){
        int remainNumber = booking.getBookingNumber() - purchasedNumber;
        booking.setRemainNumber(remainNumber);
        bookingService.updateById(booking);
        return Result.success();
    }

    @PostMapping
    public Result<?> save(@RequestBody Booking booking ,@RequestParam int purchasedNumber){
        int remainNumber = booking.getBookingNumber() - purchasedNumber;
        booking.setRemainNumber(remainNumber);
        bookingService.save(booking);
        return Result.success();
    }

    @PostMapping("/userShop")
    public Result<?> saveUserShop(@RequestBody Booking booking ,@RequestParam int adultNumber,@RequestParam int childNumber,@RequestParam int userid,@RequestParam String username,@RequestParam String seaBookingName){

        UserShop userShop = new UserShop();
        userShop.setUserId(userid);
        userShop.setUsername(username);
        userShop.setAdultNumber(adultNumber);
        userShop.setChildNumber(childNumber);
        userShop.setBookingName(seaBookingName);
        userShop.setTotalPrice(booking.getAdultPrice()*adultNumber + booking.getChildPrice()*childNumber);
        if(booking.getRemainNumber()-adultNumber-childNumber>0){
            booking.setRemainNumber(booking.getRemainNumber()-adultNumber-childNumber);
            bookingService.updateById(booking);
        }else{
            return Result.error("400","票数不足");
        }
        userShopService.save(userShop);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id){
        bookingService.removeById(id);
        return Result.success();
    }


}
